<?php
/**
*广告位管理
*
*/
class AdController extends CController
{
	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction='manage';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_post;

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image
			// this is used by the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xF5F5F5,
			),
		);
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'list' and 'show' actions
				'actions'=>array('list','show','captcha'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated users to perform any action
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Shows a particular post.
	 */
	public function actionShow()
	{
		$post=$this->loadPost();
		$comment=$this->newComment($post);
		$this->render('show',array(
			'post'=>$post,
			'comments'=>$post->comments,
			'newComment'=>$comment,
		));
	}

	/**
	 * Creates a new post.
	 * If creation is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionAdd()
	{
		$post=new Post;
		if(isset($_POST['Post']))
		{
			$post->attributes=$_POST['Post'];
			if(isset($_POST['previewPost']))
				$post->validate();
			else if(isset($_POST['submitPost']) && $post->save())
				$this->redirect(array('show','id'=>$post->id));
		}
		$this->render('add',array('post'=>$post));
	}

	/**
	 * Updates a particular post.
	 * If update is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionUpdate()
	{
		$post=$this->loadPost();
		if(isset($_POST['Post']))
		{
			$post->attributes=$_POST['Post'];
			if(isset($_POST['previewPost']))
				$post->validate();
			else if(isset($_POST['submitPost']) && $post->save())
				$this->redirect(array('show','id'=>$post->id));
		}
		$this->render('update',array('post'=>$post));
	}

	/**
	 * Deletes a particular post.
	 * If deletion is successful, the browser will be redirected to the 'list' page.
	 */
	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadPost()->delete();
			$this->redirect(array('list'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all posts.
	 */
	public function actionManage()
	{
		$criteria=new CDbCriteria;
		$criteria->condition='status='.Post::STATUS_PUBLISHED;
		$criteria->order='createTime DESC';
		$withOption=array('author');
		if(!empty($_GET['tag']))
		{
			$withOption['tagFilter']['params'][':tag']=$_GET['tag'];
			$postCount=Post::model()->with($withOption)->count($criteria);
			$criteria->distinct=true;
		}
		else
			$postCount=Post::model()->count($criteria);

		$pages=new CPagination($postCount);
		$pages->pageSize=Yii::app()->params['postsPerPage'];
		$pages->applyLimit($criteria);

		$posts=Post::model()->with($withOption)->findAll($criteria);
		$this->render('list',array(
			'posts'=>$posts,
			'pages'=>$pages,
		));
	}


}
